//
//  ProductDetailsView.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/29.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "ProductDetailsView.h"
#import "homeModel.h"
@implementation ProductDetailsView
@synthesize titleLab,addressLab,priceLab,futureLab,numberLab,purchaseBtn,salesLab;
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLab = [self createLabelFrame:CGRectMake(10, 13, SWIDTH - 20, 16) title:@"" titleColor:[UIColor blackColor] tag:0];
        self.titleLab.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.titleLab];
//        CGSize titleSize = [aString sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
        self.priceLab = [self createLabelFrame:CGRectMake(10, 50, 60, 12) title:@"" titleColor:[UIColor blackColor] tag:0];
        self.priceLab.textColor = REDColor;
        [self.priceLab setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:self.priceLab];
        
        UILabel *tempFutureLab = [self createLabelFrame:CGRectMake(self.priceLab.frame.size.width + self.priceLab.frame.origin.x+5, 48, 36, 18) title:@"现货" titleColor:[UIColor whiteColor] tag:0];
        tempFutureLab.font = [UIFont systemFontOfSize:12];
        self.futureLab = tempFutureLab;
        self.futureLab.textAlignment = NSTextAlignmentCenter;
        self.futureLab.backgroundColor = UIColorFromRGB(0x2578c3);
        [self addSubview:self.futureLab];
        
        UILabel *tempSalesLab = [self createLabelFrame:CGRectMake(self.futureLab.frame.size.width + self.futureLab.frame.origin.x+5, 48, 36, 18) title:@"特价" titleColor:[UIColor whiteColor] tag:0];
        tempSalesLab.font = [UIFont systemFontOfSize:12];
        self.salesLab = tempSalesLab;
        self.salesLab.textAlignment = NSTextAlignmentCenter;
        self.salesLab.backgroundColor = REDColor;
        [self addSubview:self.salesLab];
        
        self.purchaseBtn = [self createButtonFrame:CGRectMake(SWIDTH - 110, 41, 100, 27) title:@"我要采购" viewController:self selector:nil tag:0];
        [self.purchaseBtn setBackgroundColor:UIColorFromRGB(0xf84201)];
        [self.purchaseBtn setTitleColor:[UIColor whiteColor] forState:0];
        self.purchaseBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.purchaseBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, 74, SWIDTH-20, 1)];
        line.backgroundColor = [UIColor grayColor];
        line.alpha = 0.2;
        [self addSubview:line];
        NSString *price = @"发货地　";
        NSString *price1= @"由";
        UIFont *font = [UIFont systemFontOfSize:12];
        CGSize titleSize = [price sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
        CGSize titleSize1 = [price1 sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
        UILabel *tempAddressLab1 = [self createLabelFrame:CGRectMake(10, 88, titleSize.width, 12) title:price titleColor:GRAYColor tag:0];
        tempAddressLab1.font = [UIFont systemFontOfSize:12];
        [self addSubview:tempAddressLab1];
        
        UILabel *tempAddressLab3 = [self createLabelFrame:CGRectMake(10+titleSize.width, 88, titleSize1.width, 12) title:@"由" titleColor:[UIColor blackColor] tag:25];
        tempAddressLab3.font = [UIFont systemFontOfSize:12];
        [self addSubview:tempAddressLab3];
        
        self.addressLab = [self createLabelFrame:CGRectMake(10+titleSize.width+titleSize1.width+2, 88, SWIDTH - 20, 12) title:@"" titleColor:UIColorFromRGB(0x2578c3) tag:0];
        [self.addressLab setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:self.addressLab];
        
        UILabel *tempAddressLab2 = [self createLabelFrame:CGRectZero title:@"发货" titleColor:[UIColor blackColor] tag:20];
        tempAddressLab2.font = [UIFont systemFontOfSize:12];
        [self addSubview:tempAddressLab2];
        
        UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(10,111, SWIDTH-20, 1)];
        line1.backgroundColor = [UIColor grayColor];
        line1.alpha = 0.2;
        [self addSubview:line1];
        
        UILabel *tempAddressLab4 = [self createLabelFrame:CGRectMake(10, 122, titleSize.width, 12) title:@"库存量　" titleColor:GRAYColor tag:26];
        tempAddressLab4.font = [UIFont systemFontOfSize:12];
        [self addSubview:tempAddressLab4];
        
        self.numberLab = [self createLabelFrame:CGRectMake(10, 122, SWIDTH - 20, 12) title:@"" titleColor:[UIColor blackColor] tag:0];
        [self.numberLab setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:self.numberLab];
    }
    return self;
}

-(void)drawDataCellView:(NSMutableDictionary *)obj{
    if (![Unity isBlankString:[obj objectForKey:@"title"]]) {
        self.titleLab.text = [obj objectForKey:@"title"];
    }else{
        NSString *title = [NSString stringWithFormat:@"%@ %@",[obj objectForKey:@"brand_name"],[obj objectForKey:@"material_name"]];
        self.titleLab.text = title;
    }
    NSString *price = [NSString stringWithFormat:@"%@元/%@",[obj objectForKey:@"price"],[obj objectForKey:@"unit"]];
    self.priceLab.text = price;
    UIFont *font = [UIFont systemFontOfSize:12];
    CGSize titleSize = [price sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
    self.priceLab.frame = CGRectMake(10, 50, titleSize.width, 12);
    self.futureLab.frame = CGRectMake(self.priceLab.frame.size.width + self.priceLab.frame.origin.x+5, 48, 36, 18);
    NSString *tempNuber = @"库存量　";
    CGSize titleSize26 = [tempNuber sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
    NSString *number = [NSString stringWithFormat:@"%@%@",[obj objectForKey:@"number"],[obj objectForKey:@"unit"]];
    UILabel *lab1 = (UILabel *)[self viewWithTag:26];
    self.numberLab.frame = CGRectMake(10+titleSize26.width, 122, SWIDTH - 20-titleSize26.width, 12);
    self.numberLab.text = number;
    NSString *address = [NSString stringWithFormat:@"%@%@",[obj objectForKey:@"province_name"],[obj objectForKey:@"city_name"]];
    NSString *price1 = @"发货地　由";
    CGSize titleSize1 = [price1 sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
    CGSize titleSize2 = [address sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 12)];
    self.addressLab.frame = CGRectMake(10+titleSize1.width+2, 88, titleSize2.width, 12);
    UILabel *lab = (UILabel *)[self viewWithTag:20];
    self.addressLab.text = address;
    lab.frame = CGRectMake(self.addressLab.frame.origin.x + titleSize2.width +2, 88, 100, 12);
    if ([[obj objectForKey:@"future"]isEqualToString:@"0"]) {
        self.futureLab.backgroundColor = BLUEColor;
        self.futureLab.text = @"现货";
    }else{
        self.futureLab.backgroundColor = GREENColor;
        self.futureLab.text = @"期货";
    }
    self.salesLab.frame = CGRectMake(self.futureLab.frame.size.width + self.futureLab.frame.origin.x+5, 48, 36, 18);
    if ([[obj objectForKey:@"is_sales"] intValue]==0) {
        self.salesLab.hidden = YES;
    }else{
        self.salesLab.hidden = NO;
    }
    if (![Unity isBlankString:[obj objectForKey:@"audit_status"]]) {
        if ([[obj objectForKey:@"audit_status"] integerValue]==0) {
            [self.purchaseBtn setTitle:@"审核中" forState:0];
            [self.purchaseBtn setBackgroundColor:REDColor];
        }else if ([[obj objectForKey:@"audit_status"] integerValue]==1){
            
        }else{
            [self.purchaseBtn setTitle:@"未通过" forState:0];
            [self.purchaseBtn setBackgroundColor:UIColorFromRGB(0xbfbfbf)];
        }
    }
}

//创建UILabel  tag值为三位数
- (UILabel *)createLabelFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)color
                          tag:(NSInteger)tag{
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:frame];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setText:title];
    [tempLabel setTag:tag];
    [tempLabel setTextColor:color];
    return tempLabel;
}
//创建UIButton tag值为四位数
- (UIButton *)createButtonFrame:(CGRect)frame
                          title:(NSString *)title
                 viewController:(id)viewController
                       selector:(SEL)selector
                            tag:(NSInteger)tag{
    UIButton *tempButton = [UIButton buttonWithType:0];
    [tempButton setFrame:frame];
    [tempButton setTag:tag];
    [tempButton setBackgroundColor:[UIColor clearColor]];
    [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton addTarget:viewController action:selector forControlEvents:UIControlEventTouchUpInside];
    return tempButton;
}
//创建UIImageView tag值为五位数
- (UIImageView *)createImageViewFrame:(CGRect)frame
                                image:(UIImage *)image
                            imageName:(NSString *)imageName
                                  tag:(NSInteger)tag{
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:frame];
    [tempImageView setBackgroundColor:[UIColor clearColor]];
    if (image) {
        tempImageView.image = image;
    }else{
        tempImageView.image = [UIImage imageNamed:imageName];
    }
    [tempImageView setTag:tag];
    return tempImageView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
